package com.dj.product.k2faas.view.function;

import com.dj.product.k2faas.entity.function.Function;
import com.dj.product.k2faas.common.exception.NotFoundException;
import com.dj.product.k2faas.jpa.PredicateWrapperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

/**
 * Created by 丁建 on 2025/9/23 16:36
 *
 * @author 丁建
 */
@Component
class DefaultFunctionView implements FunctionView {

    @Autowired
    private FunctionRepository functionRepository;

    @Override
    public Function findByName(String name) {
        return functionRepository.findByName(name).orElseThrow(() -> new NotFoundException("未找到名称为：%s的方法".formatted(name)));
    }

    @Override
    public Function save(Function fct) {
        return functionRepository.save(fct);
    }

    @Override
    public Function findById(long id) {
        return functionRepository.findById(id).orElseThrow(() -> new NotFoundException("未找到ID为：%s的方法".formatted(id)));
    }

    @Override
    public Page<Function> findAll(String keyword, Pageable pageable) {
        var spec = PredicateWrapperBuilder.orBuilder(Function.class)
                                          .innerLike(Function::getName, keyword)
                                          .innerLike(Function::getDescription, keyword)
                                          .innerLike(Function::getContent, keyword)
                                          .build();
        return functionRepository.findAll(spec, pageable);
    }
}
